// pages/businessList/businessList.js
import { fetchBusiness } from '../../api/Business'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        value: '',
        businessList: [],
        flag: true,
        page: 1,
        pageSize: 5,
        total: 0,
        loading: true
    },

    async getBusinessList() {
        wx.showLoading({
            title: '数据加载中...'
        })
        try {
            let res = await fetchBusiness({
                _page: this.data.page,
                _limit: this.data.pageSize
            })
            this.setData({
                businessList: [...this.data.businessList, ...res.data],
                total: res.header['count'] - 0
            })
        } catch (err) {
            console.log(err)
        }
        wx.hideLoading()
        this.setData({
            loading: false
        })
        wx.stopPullDownRefresh()
    },

    details(e) {
        if (e.currentTarget.dataset.info == 0) {
            wx.navigateTo({
                url: '../serveDetails/serveDetails'
            })
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getBusinessList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.setData({
            page: 1,
            businessList: [],
            total: 0,
            flag: true
        })
        this.getBusinessList()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (this.data.page * this.data.pageSize >= this.data.total) {
            this.setData({
                flag: false,
            })
        } else {
            this.setData({
                page: this.data.page + 1,
                total: this.data.businessList.length
            })
            this.getBusinessList()
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})